package com.example.hotel.web;

import com.example.hotel.common.JsonResult;
import com.example.hotel.entity.Dish;
import com.example.hotel.service.DishService;
import com.example.hotel.service.impl.DishServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "获取菜品信息")
@RestController
@CrossOrigin
@RequestMapping("dish")
public class DishController {
    @Autowired
    DishService service;

    @ApiOperation(value = "添加菜品")
    @GetMapping("aged")
    public JsonResult addDish(Dish d) {
        try {
            int r = service.addDish(d);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "菜品添加失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }
    }

    @ApiOperation(value = "删除菜品")
    @DeleteMapping("delete")
    public JsonResult deleteDish( Integer dishID) {
        try {
            if (dishID == null) {
                return JsonResult.fail(5001, "菜品id不能为空");
            }
            int r = service.deleteDish(dishID);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "菜品删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }
    }

    @ApiOperation(value = "更新菜品")
    @GetMapping("update")
    public JsonResult updateDishPrice(@ApiParam(name = "dishID", value = "1", required = true)Integer dishID,@ApiParam(name = "price", value = "菜品价格", required = true)double price) {
        try {
            int r = service.updateDishPrice(dishID, price);
           if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "菜品更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }

    }

    @ApiOperation(value = "查询菜品")
    @GetMapping("getAll")
    public JsonResult getAllDish() {
        try {
            List<Dish> dishes=  service.getAllDish();

            if (!dishes.isEmpty()) {
                return JsonResult.ok(dishes);
            } else {
                return JsonResult.fail(5001, "菜品查询失败");
            }
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();}
    }

}
